import dayjs from "dayjs";
import { IGlobalConfig, IPhotoData, IPhotoInfo } from "../../type";
import { computeSceneConfig } from "../../utils/compute";
import { brandLogo } from "../../config/option";

const assemblyData = (
  imgDom: any,
  name: string,
  globalConfig: IGlobalConfig
): IPhotoData => {
  const {
    Make, // 厂家
    Model, // 机型
    FNumber, // 光圈
    ExposureTime, // 快门
    ISOSpeedRatings, // iso
    DateTimeOriginal, // 时间
    // DateTime, // 时间
    // PixelXDimension, // 分辨率X
    // PixelYDimension, // 分辨率Y
    FocalLength, // 焦距
    FocalLengthIn35mmFilm, // 等效焦距
  } = imgDom.exifdata;

  console.log("🚀 ~ 照片信息:", imgDom.exifdata);

  // 计算快门
  const shutter = (() => {
    if (!ExposureTime) {
      return "";
    } else if (ExposureTime >= 1 || !ExposureTime?.denominator) {
      return `${ExposureTime}″`;
    } else {
      return `1/${(ExposureTime.denominator / ExposureTime.numerator).toFixed(
        0
      )}`;
    }
  })();

  const photoInfo: IPhotoInfo = {
    make: Make,
    model: Model,
    isSign: globalConfig.isSign,
    logo: brandLogo.find((item) => item.sign.includes(Make))?.value || "",
    date: dayjs(DateTimeOriginal ?? dayjs(), "YYYY:MM:DD HH:mm:ss").format(
      globalConfig.dateFormat
    ),
    customLogo: globalConfig.customLogo,
    width: imgDom.width,
    height: imgDom.height,
    direction: imgDom.width < imgDom.height ? "vertical" : "transverse",
    iso: ISOSpeedRatings,
    f: FNumber,
    address: globalConfig.address,
    scale: globalConfig.scale,
    padding: globalConfig.padding,
    shutter,
    focus: FocalLength,
    realFocus: FocalLengthIn35mmFilm,
  };

  return {
    id: new Date().getTime(),
    name,
    originalPhoto: imgDom.src,
    photoInfo,
    sceneConfig: computeSceneConfig(photoInfo, globalConfig),
    globalConfig,
  };
};

export { assemblyData };
